package biz;

import dao.ChangePwdDao;
import dao.UserDao;

import java.util.Map;

public class UserBiz {

    private UserDao dao = new UserDao();
    //用户登录判断
    public Map<String,Object> UserLogin(String name, String pwd) throws BizException {

        if (name == null || name.trim().equals("")){
            throw new BizException("用户名不能为空");
        }

        if (pwd==null || pwd.trim().equals("")){
            throw  new BizException("密码不能为空");
        }

        Map<String,Object> user = dao.userLogin(name,pwd);
        if (user ==null){
            throw new BizException("用户名或密码错误!请确认后重新输入...");
        }
        return user;
    }

    // 用户 添加数据判断
    public int saveUser(String uname, String pwd, String phone, String sex, String email,String image) throws BizException {
        if ("".equals(uname) || uname == null){
            throw new BizException("用户名不能为空！");
        }
        final Map<String, Object> user = dao.findUser(uname);
        if (user!=null){
            throw new BizException("该用户名已存在,请重新填写");
        }
        isData( pwd, phone, sex, email);

        return dao.saveUser(uname, pwd, phone, sex, email, image);
    }

    // 用户 修改数据判断
    public int modifyUser(String uname, String pwd, String phone, String sex, String email, String uid) throws BizException {
        if ("".equals(uname) || uname == null){
            throw new BizException("用户名不能为空！");
        }

        final Map<String, Object> user = dao.findUser(uname);
        if (user!=null){
            if (!uid.trim().equals(user.get("uid").toString())){
                throw new BizException("该用户名已存在,请重新填写");
            }
        }

        isData( pwd, phone, sex, email);

        return dao.modifyUser(uname, pwd, phone, sex, email, uid);
    }

    private void isData(String pwd, String phone, String sex, String email) throws BizException {



        //根据名字查询数据库是否有数据，如果为空则不存在数据

        if (pwd.length() < 6 || pwd.trim().length() != pwd.length()){
            throw new BizException("密码不能有空格且至少六个字符！");
        }


        if (!email.matches("\\d{10}\\@qq.com")){
            throw new BizException("邮箱的格式必须为10个数字加@qq.com后缀");
        }

        if (phone.trim().length()!=11){
            throw new BizException("电话号码必须为11位");
        }

        if ("".equals(sex) || sex == null){
            throw new BizException("性别不能为空！");
        }
    }

    public int changePwd(String oldpwd,String newpwd,String againpwd,String uid) throws BizException{


        if(oldpwd == null  || "".equals(oldpwd.trim())){
            throw new BizException("旧密码不能为空");
        }

        if(newpwd == null  || "".equals(newpwd.trim())){
            throw new BizException("新密码不能为空");
        }

        if(againpwd == null  || "".equals(againpwd.trim())){
            throw new BizException("密码不能为空");
        }

        if( newpwd.trim().equals(oldpwd.trim())){
            throw new BizException("新旧密码不一致");
        }

        if(newpwd.trim().equals(againpwd.trim())){
            throw new BizException("新密码与确认密码不一致");
        }




        ChangePwdDao dao1 = new ChangePwdDao();
        return dao1.modifyUser(newpwd,uid);
    }

}
